﻿using BW.IDAL;
using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;

namespace BW.DAL
{
    public class BaseDAL<T>:IBAseDAL<T> where T : class
    {
        MYDbContext db;
        public BaseDAL(MYDbContext db)
        {
            this.db = db;
        }

        public async Task<int> AddListAsync(List<T> list)
        {
            await db.Set<T>().AddRangeAsync(list);
            return await db.SaveChangesAsync();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> GetAdd(T t)
        {
            await db.Set<T>().AddAsync(t);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetAll()
        {
            return db.Set<T>().AsQueryable();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> GetUpdate(T t)
        {
             db.Set<T>().AddAsync(t);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public async Task<T> GetValue(Expression<Func<T, bool>> expression)
        {
            return await db.Set<T>().Where(expression).FirstOrDefaultAsync();
        }
    }
}
